package op3;

import java.util.Scanner;

public class NRU {
    private static final int m = 5;
    private static final int n = 3;
    static double interrupt = 0;
    static int k = 0;
    static int[] pageTable = new int[m];
    static int[] block = new int[n];
    static int[] process = new int[20];

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int i = 0;
        for(i = 0; ; i++){
            int input = in.nextInt();
            if(input == 0) break;
            process[i] = input;
        }
        for(int j=0; j<i; j++){
            visit(process[j]);
            System.out.println("共"+i+"次访问，产生"+interrupt+"次缺页中断，缺页率为 "+interrupt/i);
        }
    }
    static void visit(int x){
        int i = 0;
        for(i=0; i<n; i++){
            if(block[i] == 0){
                interrupt += 1;
                pageTable[x-1] = 1;
                block[i] = x;
                System.out.print("缺页中断 内存未满 调入页 "+x+" 物理块页为 ");
                for(int j=0; j<=i; j++){
                    System.out.print(block[j]+" ");
                }
                System.out.println();
            }else if(pageTable[x-1] == 1){
                System.out.println("命中物理块内的页为 ");
                for(int j=0; j<n && block[j]!=0; j++){
                    System.out.print(block[j]+" ");
                }
                System.out.println();
            }
        }
        System.out.print("缺页中断 淘汰页"+block[k]+" 调入页"+x+" 物理块内的页为 ");
        interrupt += 1;
        pageTable[block[k]-1] = 0;
        block[k] = x;
        pageTable[x-1] = 1;
        k = (k + 1) % n;
        for(int j=0; j<n; j++){
            System.out.print(block[j]+" ");
        }
        System.out.println();
    }
}
